package com.mhuang.module.wechat.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.mhuang.common.utils.network.NetWorkUtils;
import com.mhuang.module.wechat.config.WechatConfig;
import com.mhuang.module.wechat.service.SubscribeService;
import com.mhuang.module.wechat.service.WechatConfigService;

/**
 * 
 * @Description 关注service实现
 * @author mHuang
 * @version V1.0.0
 */
@Component
public class SubscribeServiceImpl implements SubscribeService{

	public Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private WechatConfigService wechatConfigService;
	
	@Autowired
	private WechatConfig config;
	
	
	/**
	 * 
	 * @Description 存放数据
	 * @author mHuang
	 * @param jsonObj
	 */
	public void subscribe(String openId,String status){
		String usr = NetWorkUtils.sync(
				config.getWechat_getuser_url().
				replace("ACCESS_TOKEN", wechatConfigService.getToken()).
				replace("OPENID", (CharSequence) openId));
		String nickname = (String) JSON.parseObject(usr).get("nickname");
		String headimgurl = (String) JSON.parseObject(usr).get("headimgurl");
		if(querySubscribe(openId) == 0){
			insertSubscribe(openId, nickname,headimgurl,status);
		}else{
			updateSubscribe(openId,nickname,headimgurl, status);
		}
	}
	
	
	public int insertSubscribe(Object openId,String nickName,String headimgurl,String status){
		String sql = "insert into wechat_subscribe(openId,nickname,subscribe,headimgurl,subscribeTime) values(?,?,?,?,now())";
		return jdbcTemplate.update(sql, new Object[]{openId,nickName,status,headimgurl});
	}
	
	public int updateSubscribe(Object openId,String nickname,String headimgurl,String status){
		String sql = "update wechat_subscribe set nickname=?,subscribe=?,headimgurl=?,subscribeTime=now() where openId=?";
		return jdbcTemplate.update(sql, new Object[]{nickname,status,headimgurl,openId});
	}
	
	public int insertSubscribe2(Object openId,String nickName,String headimgurl,String status,String qrcode,Object school_id){
        String sql = "insert into wechat_subscribe(openId,nickname,subscribe,headimgurl,parent_id,school_id,subscribeTime) values(?,?,?,?,?,?,now())";
        return jdbcTemplate.update(sql, new Object[]{openId,nickName,status,headimgurl,qrcode,school_id});
    }
    
    public int updateSubscribe2(Object openId,String nickname,String headimgurl,String status,String qrcode,Object school_id){
        String sql = "update wechat_subscribe set nickname=?,subscribe=?,headimgurl=?,parent_id=?,school_id=?,subscribeTime=now() where openId=?";
        return jdbcTemplate.update(sql, new Object[]{nickname,status,headimgurl,qrcode,school_id,openId});
    }
    
	public Long querySubscribe(Object openId){
		String sql = "select count(*) from wechat_subscribe where openId = ?";
		return jdbcTemplate.queryForObject(sql,new Object[]{openId},Long.class);
	}
	
	public Map<String, Object> getSubscribe(Object openId){
	    String sql = "select * from wechat_subscribe where openId = ?";
	    try{
	        return jdbcTemplate.queryForMap(sql,new Object[]{openId});
	    }catch(Exception e){
	        Map<String, Object> map = new HashMap<>();
	        map.put("school_id", "");
	        return map;
	    }
	}


    @Override
    public void subscribeOtherEvent(String openId,String status, String eventKey) {
    }
    


    @Override
    public Map<String, Object> queryForOpenId(String openId) {
        String sql = "select * from wechat_subscribe where openId = ?";
        try{
            return jdbcTemplate.queryForMap(sql,new Object[]{openId});
        }catch(Exception e){
            return null;
        }
    }


	@Override
	public void scan(String openId, String eventKey) {
	}
}
